<?php
class VoteAction extends BaseAction
{
    public function _initialize()
    {
        parent::_initialize();
        $this->assign('nav', 2);
    }

    public function index()
    {
        //获取开始投票的投票列表
        $VoteModel = D('Vote');
        $nowCount = $VoteModel->getNowCount();
        if ($nowCount) {
            $Page = $this->getPage($nowCount);
            $votes = $VoteModel->getNowList(null, $Page->firstRow . ',' . $Page->listRows);
            $this->assign('votes', $votes);
            $this->assign('page', $Page->show());
        }
        $this->display();
    }

    /**
     * 获取投票详细
     * @author Ice <iceinto@mallog.com.cn>
     */
    public function detail()
    {
        $itemid = intval($this->_get('id'));
        $key = hh($this->_get('key'));
        if (empty($itemid)) {
            $this->error('投票信息错误！');
        }
        $vote = M('Vote')->find($itemid);
        if (empty($vote)) {
            $this->error('投票信息不对！');
        }

        //判断类型是不是审核类型 审核类型必须 有关键字才可以
        if ($vote['type'] == 1) {
            if (empty($key)) {
                //只能查看
            } else {
                //判断关键字是否正常
                $key = explode('|', decrypt($key));
                if (empty($key) || (count($key) < 2)) {
                    $this->error('邀请信息错误！');
                }
                $VrModel = D('VoteRecord');
                //核对
                $map = array();
                $map['itemid'] = $key[1];
                $map['email'] = $key[0];
                $vr = $VrModel->where($map)->find();
                //记录是否可以投票
                $canRecord = trun;
                if ($vr) {
                    //判断是否投过票
                    if ($vr['status'] != 1) {
                        $canRecord = false;
                    }
                } else {
                    $this->error('关键信息不对！');
                }

                //判断是否是表单提交
                if ($this->isPost()) {
                    $this->record($vote, $vr);
                }
            }
            //获取主题投票 情况
            $subjects = array();
            $allVotes = 0;
            for ($i = 1; $i <= 10; $i++) {
                if (!empty($vote['s' . $i])) {
                    $subjects[] = array('key' => $i, 'title' => $vote['s' . $i], 'count' => $vote['v' . $i]);
                    $allVotes += $vote['v' . $i];
                } else {
                    brack;
                }
            }
            $this->assign('all_votes', $allVotes);
            $this->assign('subjects', $subjects);
            $this->assign('can_record', $canRecord);
        }
        //如果投票结束 显示投票 内容
        if($vote['end_time'] ){
            $map = array();
            $map['itemid'] = $itemid;
            $map['status'] = 100;
            $VrModel = D('VoteRecord');
            $rCount = $VrModel->getWhereCount($map);
            if ($rCount) {
                $Page = $this->getPage($rCount);
                $vrs = $VrModel->getList($map, $Page->firstRow . ',' . $Page->listRows);
                $this->assign('vrs', $vrs);
                $this->assign('page', $Page->show());
            }
        }
        //统计点击率
        $voteViewTime = intval(Cookie::get('vote_view_time'));
        if ($voteViewTime < (time() - 60)) {
            Cookie::set('vote_view_time', time());
            //
            $data['itemid'] = $itemid;
            $data['hits'] = array('exp', 'hits+1');
            M('Vote')->save($data);
        }
        $this->assign('v', $vote);
        $this->display();
    }

    /**
     * 进行投票记录
     * @param $vote
     * @param $vc
     * @author Ice <iceinto@mallog.com.cn>
     */
    private function record(&$vote, &$vr)
    {
        $key = trim($this->_post('s'));
        $note = trim($this->_post('note'));
        if (empty($note)) {
            $this->error('请认真填写你的备注！尤其是你有意见的！多谢合作！');
        }
        $s = $vote['s' . $key];
        if (!empty($s)) {
            $VoteModel = D('Vote');
            $data = array();
            $itemid = $vote['itemid'];
            //处理投票结果 添加投票次数
            $data['itemid'] = $itemid;
            $data['votes'] = array('exp', 'votes+1');
            $data['v' . $key] = array('exp', 'v' . $key . '+1');
            if ($VoteModel->save($data)) {
                $rs = false;
                if ($vote['type'] == 1) {
                    //修改投票记录
                    $rdata = array();
                    $rdata['rid'] = $vr['rid'];
                    $rdata['ip'] = get_client_ip();
                    $rdata['vote_time'] = time();
                    $rdata['votes'] = $key;
                    $rdata['status'] = 100;
                    $rdata['note'] = $note;
                    $rs = M('VoteRecord')->save($rdata);
                } else {
                    //添加投票记录
                    $rdata = array();
                    $rdata['itemid'] = $itemid;
                    $rdata['ip'] = get_client_ip();
                    $rdata['vote_time'] = time();
                    $rdata['votes'] = $key;
                    $rdata['status'] = 100;
                    $rdata['note'] = $note;
                    $rs = M('VoteRecord')->add($rdata);
                }
                if ($rs) {
                    //TODO 添加对数据统计
                    $this->success('投票成功！');
                    exit;
                } else {
                    //失败时候重新返回操作
                    $data['votes'] = array('exp', 'votes-1');
                    $data['v' . $key] = array('exp', 'v' . $key . '-1');
                    $VoteModel->save($data);
                }
                $vote = $VoteModel->find($itemid);
            }
        } else {
            $this->error('投票结果不对！');
        }
    }
}